package com.start.security.data.encryption.sm2;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;


public class Sm2Util {
    /**
     * 生成秘钥对
     *
     * @return 公钥和私钥
     */
    public static Sm2KeyModel generator() {
        SM2 sm2 = SmUtil.sm2();
        String publicKey = HexUtil.encodeHexStr(((BCECPublicKey) sm2.getPublicKey()).getQ().getEncoded(false)).toUpperCase();
        String privateKey = HexUtil.encodeHexStr(BCUtil.encodeECPrivateKey(sm2.getPrivateKey())).toUpperCase();
        Sm2KeyModel sm2KeyModel = new Sm2KeyModel();
        sm2KeyModel.setPrivateKey(privateKey);
        sm2KeyModel.setPublicKey(publicKey);
        return sm2KeyModel;
    }

    /**
     * 加密
     *
     * @param publicKey 公钥
     * @param data      明文
     * @return 密文
     */
    public static String encrypt(String data, String publicKey) {
        return SmUtil.sm2(null, publicKey)
                // 不写默认就是C1C3C2
                .setMode(SM2Engine.Mode.C1C3C2)
                .encryptHex(data.getBytes(), KeyType.PublicKey)
                // 加密后，密文前面会有04，需要去掉
                .substring(2);
    }

    /**
     * 解密
     *
     * @param privateKey 私钥
     * @param data       密文
     * @return 明文
     */
    public static String decrypt(String data, String privateKey) {
        // 确定前端不会加04，所以后端直接加（上面处理方式可能造成报错（Invalid point coordinates）：原因前端加密后密文自带04开头）
        data = "04" + data;
        return SmUtil.sm2(privateKey, null)
                // 不写默认就是C1C3C2
                .setMode(SM2Engine.Mode.C1C3C2)
                .decryptStr(data, KeyType.PrivateKey);
    }
    private static String decrypt(Sm2KeyModel sm2KeyModel, String data) {
        //        System.out.println("解密后数据：" + decryptData);
        return decrypt(data, sm2KeyModel.getPrivateKey());
    }

    private static String encrypt(Sm2KeyModel sm2KeyModel, String data) {
        return encrypt(data, sm2KeyModel.getPublicKey());
    }

    public static void main(String[] args) {
        System.out.println("=====================java=========================");
        testJava();
        System.out.println("=====================VUE=========================");
        testVue();
    }

    private static void testVue() {
        Sm2KeyModel sm2KeyModel = new Sm2KeyModel();
        sm2KeyModel.setPrivateKey("3a148c4ed0577b7d18a3c54643423de3948cd76d051b18e0de5cef97fb2a3277");
        sm2KeyModel.setPublicKey("045c55acb8a4de257078b389fc930fadff3cbbc1c23828654ce1ddd4f8d8b8cff590dd1679992a424f68f132751844b0064fd0d6862035fef99426bb008f939741");
        String data = "92b7e6a985a84f270a8e02dfd8092f8fd0ca74095c96dc99d6a08c3662ca524ab43352420a064cfdb5e4a3cd6b56ad6182b5e0732d2f25751805724638864fcca13a60672a2bfcce000289204137f70afb839d9f894f5636fcc31aad870691b767fd9fe49f253ceaac2c34e2882e84257c08caaa82750784b30cf95bbf9eca8fef3dca8efb1f471467767c7fd063b9351d45d2cd00731c18f58caa0bad9dbf2a8f59ad638157aa764285dac1416d861cfc82d3785c742f4f84ddf846e5c94ffb3906fecf3f9fd294379143dbde0d061d13f6b0283ecd4a49ce69973c5d5f003508a82a770f5edcf76589ec12a75bd316465a0b9a2344f665574cdce8b6730261e51232026ba3e4c8d2ce1c8839910d0a0e6ac45a838b61cf86b48a85e6521c7d30801e511289f972095cd4394206839ffc06c97951679f1c38bb52ffc0285f88ddf49bd778d32a20763a25e519fdc73bd6c0e59ad203647beed62fd7475595bf0f32c90408d8fc94170df47b71f3b471f4341fea97dd851e8a7ab0c1c2f910be0a7b555486cb440c9a0d601b3c1f7b049570d3ea122b9917f9023907e64e810b83fdf04fd1539ed207c9dbb73863c4d5e00984a13dad3df267049f4be3aadb38fcc565f5ecffd45f4071e34421da1784b0d0fc5d81acc2acaa5cdd77f8bf785abbf893642567f2a8076b21021ddef8a50275e54bafdf2ff10573b52f0d69c187e394c865350ed6e1c5cbdbeb389b01567a4c14a1b7715bd3301faf9c576d7eecee3935bcc35b4c3ccb5525fdfca2ae633c96ab9cbc474d02bebe263ec6c07da2b0a244af560d8d1910a8319595cdfc06e9ca2365091cd9de5e8d3d2bf4d73c301b2b27b166875d81807840f15c9a37f17bbba7bad82b7b0d5d0d64ee8807928aa75edb72716dd6ca795130fb7362222cde4753bffc8c819af4bfe572bffb37f22be593f20c561eb1ac1d27754c9096a2c0b13847f321965a7ebd244d2eaea2f22a89e16ed48bccc0bac78994188d32b80ab93bd78d3e16e3f547fa784e07f999ecc833c334ef47665e6fb5615603f0f598dc411403d79503a84ae8825b5dc92a9d7babf319377c2aefe522d452fb0fed4c7809cfebed50991d5ac2f29378c829c03ddbf02ecc16c08fe6f778c2b436d3ee41b367c80acfd7d918e00620d260498bd045cf895b80a20c3b2544dd2b359f6e4b65aeebda8de58bc1f583101be40276710eb92fa27831c46ea2984a4bb2cc282ff575bfa3bd13a7dc25aa163198195030a1e7f1955c83a2d2446f4b8eb44e90b2c6ee0c65a7c89edc8971fb2edf0c2bb728dc87a8643eae25ecb7a2a91c624fff16aa16d62677cbe66b48ebec0911fc94edeed47928e53b6538a2d1fcc76d97102d7a05969361c780f948bc9f9812177e03eb3525067218593eeb0b737138a8aad157d980ced33e918b4f9bdbc54dd056e737a6ed08ca95511c9842812f5412029b2075570223c32f97dec74a5d2375652b8ab354be19a2018973eb453c66c078bd70e775acbc234ad35f9b472d8a81c42b2036cce1a80c5b43ab0170b15a780a98d1563dd41501a9f1daa2359e05778dbc9c43ef89d573f4ce39d82e809908642fe67ee81de5d5a9e07e3bb06bb046bcf016982e86d45196321e3c115c7284919b581a14c94d664cbe89e3b558e0db227710fe763668e1cfd38a3892e16072b1a6d96150171af151a8661a439a0613a44f46ec83adc756529bde8872beccdfdedc71a00f8de6e4603e14d5151b214f8fe857ae86242c47f7b708f6e6f1433c6f7d791dea825ca13022016b1841ec84a40a82b836da004c867783783a3bc6adecd4bab4d192e35bd918951cea513d20b72aa35b8164c6b8d4908bdcceb4b18a314e04bee9b3e14312bf91af08ac3deac1f812622303f55620ff69a07abc6859b6b8d6bdda885d05855ed65bf3b4eb55cc78f96be253481dd8f2862d4feb1fbcc8139eafcfe4a90c588f31ff2f5598ea1daef01c9e293794baa07d02ea082b640d16db7aa193a47f89016b1e839759f4e6eb8fb10d8d116c28cbc746a0454885ccef8c5fed93da858f68734ec207416e787b11d806b612ddb08997df4f8282225b00fc055ff5ba7edfad1d0d1943f1c27ed9d0567ccbce35f596501015cd5670cbcc1c23bc5b25ef20feba1eb60083c4ea4daa222da4bd568d06d2d5c991b06eca2ce9ed6a4a5dbbac4a08034220c25b4118291eaa72aa2e132924f09fff259f346677a7e65f996e317e56f65da92b6349230eb45ff955d9cd02960da7780db1769bff9ea19808443a29710497bcbf06c3b1b6a560eef7dcf4cacc771edd7d99e23d1bfba359e84c69e830b1185e3c20bf6b66f7383fc778f36e859feb12b85fa9f1970d3e2ce81eeff8791b1d3631f077108d5571f03c379296932febb57e8f48b630e970c1c659eda81293ce1ba77328cb34088130d67c1f0c34b96b37f5c4cc05af2c3beecd4fb14e9b737a2380b36d703805a9639bab7c47a0fa20d8a6033df5018f7b00c085fe48f5ddc8385ae3bb89efa6437ecee9cbff0366a50851ed1dcb18e0c280926aaeb8665a117969778ff76bfca10860e62248666c6ce6a6855f7acc777d5ca6805e807e250a4d6657ed9468c9313d201a8fbe8c8c0f6c1bcc457dd71805cb77dad1eaef618e63466cca7b573ac7a82a9ee2b431d5c468f46f1b02361e8968a38872815d28c6392c5a41d4820079ca482a0839ee02ba83ebf1e425b86c803ee59bace74cbb7cc49f0bf4795605079b63061b3f57a625fc05f4506213fe3341f5c3ddf0986d89e8292b98c4b3d95f08c9521693f0948e38918ba6608071bfbe3f2b5131c295ee211dd491dda49b0e10cd24068a58184eeeb499c142dc9589356c42cacc89e8f143e3d3f8d5970230fba64af4056da70cffaae4f2b4c3b30432dac6fd7fbfcbc8f9c3e7e43e9ae96b02d7ba3d26548bd703ece1071a00a643664c47be7f70eed591d8ed3f119c8d200999d2a2dbf4553d3fcc987cc80a034b2be1876dc5423b769f03e40e59b0d8226f36d7cb0b8221bb1517f552c206fcae87c60020b2fba316cf1c7f4ff775b0fdebb7f2fae1e2df7f9545b79d0eb94fbacc2ff6792da5343ff28790a28b5325914162578e4f6d2a9c40019fdb1b769bd79458bbd4ce1fe9e4bdb48abe013eebda6d16d2fa3a52ea719025433ecb570ed9075085fa6ab5cc97395c478abd8c045b3e7a216fdce4af22d7f3c33435fcbeb889292f98cec8065d4e23ba4e9dd7c16b7f69578db53c88bd67e304028ae4bfa73fee9443a1d726407c2c9d45d7fc319031193422ccdf8f2d31bb71508bb4898749a540c384dd0d47270b5b2e07f98adceec10f391d4e0312e8e9727bf932a142a2149c29082bb5e012273c8260064f444ef71ba4cae050ee7b9e35d44d7f0aa5b9d9be2f5b31158f500edc0ea8e2b61ea652f02f1959d15b925311f82071ef87d8f9057401e7842cec9f930b8703dc69961e9546705d35f371ff7dfde929d517bc0af481de0eadc4f4331d3a8839ace49b6981e0a8aea8250fbb72dafaa4b967a329ae58994f64cc74d479a21f7118e3fb165a3510c0752dbdc17484289b11632327e6815aaad3079ed6285e18a4cbe187172c9dd2fd25f444f543ddbb705706251987fe119229b6ab3f25a479574a7ff63bb39ac0992879d8765e134ea2719b158a8bb0f5f81b118629b8cff8fbce9844e4735f24c9dda53890ad1d95fb51d77328900ce689f21bda984d5c7e1a8046befe0d2f7342ddfa9ce6963b6d10ab334111278ff6ce4760b85cbfb418b50e16a1090f822572b761c5f284c928e576d26564741c71f5c2d89bb9c14f0a1601144d10b0bf9bdc3075d148dc646ba13054fb5f2667d90c04989e19fc04f4ea87fbf74e3010e8ef096e2dc05da7c72e611c313840b0ce41970984ed252ba83527f99c46dcef9d08d58789601d779050b38dcaea999ffeab0225017f5fdd0363f11b3f22e285ea797f0488421c884419c5bc5a43be860a81d78f5c7c3a59a324c19335895be427aef7311560408e0f85e5a359c423cbb374592e02c31d46b0a81e1db5957aa08945da812ee963e890795ea06380d23f33eb1a13c8a60c9b941008dd5eb55eea105d69e59edb9c827f639f35451bda0bbd1cbd49d316c7fc09d42628449763eb9c80d667d7193d1feab3078f0083bcbc3e03bf58934752dfbf828d203bcffe5b3ccc8a87d22d2bba624205cfd77e99e3453cefc8b25c33c944d2b12fcd6c1ad5886f1123b09770d2c706c39d7a867cdcdfbf9135f8e3d048415d1679388f14fab666c9e07b1a86f76d00ac4717164c588c258446baad2546acc01214eca38952598bb891db6ffc5d21e63aebe70963443e1b6f176d973cf1376eaa48feb4d60d289673b5a07c2200774689f67c28d639262e9ec406ead28bc3a64766126ad13bf185f71df73b9e90f44f20cd125da7924d85519da552b4ded7e14992821d8f457826c008d447fd25db2843486e77f601babc0045115d4b63ff6b7f82d9064a43b9376766133f85c0af3d4f80ec6ce8533de7c72684e69f5462f069fbf72bcd394533335496f5c65455415dc890ee9c64e0600722ea81e729d892ee7e5fba392b100e89c1d919e749c7d0fced25170f49065b8839693716e75d94a4c513ba631956f5330999765add81c3da58bf746228a57bdd80359113be75ae6c3bf6f1f31709387cf2b96fdb0899a48a223ec0b94356632a74a8c0867d42e4d15df38ab2da2b3ea162aa352096bfbd547f2044f0e5b3245129c3f843e8a429dc35ae9702e755bac97aff172e47960cd9e4f4b53521e3ac0bf9aadcfb184f231efb4768762f9fc350ca4a0df3ff859cb3b6da05f434bbbd07c9d1cba63cd0a13462461980c7ae7dcdfadfb80236de87fdbcc0dcb5d06533682669e09e1e2f610244f9574d64d6d47adcb185508ba406f7fd05bec11d1ba16328f859f8b93c50352d5677d12e523f258ae83010e376d2d1ceddbb54e8d0e53bfb73530fef6b33f197297bab3a5e0abab5026651245064f091cee389390b4dfb30528ee330a851514fc2817120d044e48b325a85cc3fb2870e8a599202c050549f131d12d333832902ca1baf03e74d38a7ede21f5be175f1c46fba635a302de45f13cd8d7ab8135acd8328454529480c9779abf9e05ffe35fbb7352170d6b86f8459051f4826f91b8c683bc50560105f729b7ec71ca4b99a111e9467915602538a06374ed215ce07026ddeba714af01ff880ba3608eb290c75f0d212c42215113e2ceb19cdd1c7e0561ddc13758136669a9b93d835728e42287fc2c38ca773edd2a894f4d52f89854802a7eba57e19eeb7cced6a20dea9eb92b69a676c54b42225b5308550cb0ffed2aecc013f30171f81b556a9d6e80d6ac4f93e2599ed85ac300f35913870e917afc1863bde9ac37ddf977940e3950c47a54ebc6f3d4d42f98dba2e5e8090ad30a500a0456e9f5c83876166842ec41268debb65fff78cc121c105d3d8318939f8066085a5f099ed5340abf0cc0bc50c1181db20fc5e24fe12a88683ddedf0896352a1d3b723af5eca2e78cd04ef6283ca8bada9309e21bac88411ca90dd5692a0ac436e68c053a243525c84259d50147fe85846c6033038966f13e9fbf8a6452641fdf1e485fd3b95f4ac6a23cf499f269d8152f9cc84beea5f7288569c2e7cc4056842c1841bf3483d1813361714af0ceac439ce9e81ba63b2af35aa03df2f0457e5934091b25a7f8abc4d9042f280c3604ba5bd1e3cfe6bec19967e4787ce4921d98a779a7d1a777e215721d660bb02a4c0879f49ffb6b08f45a3300f8665d2fb5412d3e51cad37d4e367b453d1a94701ff641ec956d9974d4519c582ab3659c3a935e73b5ec017196bd01d18d1dfbff3f104624d21724cdd52b3c9fec46ba57e78d5cb58f1b9b5385eeaa9637c65b870fa40d14087b5df945af58e5cea83f404ea945be86d61023b5e0e76bf37fe3ea87173ef1a1edebad68ace8a87c857e302d31d8ec6a77ae926581a69a5c6ba92d054260e8968a55925945b3597d5e9a46bd0b6ef476606af564e54257971d67c97b93066a36bbef29560fee523cd88b8cb9f4eef05d0578fe66c0c9ebc925add84894c61db1e37fc3e6f0d5586b7a732ac4e0e48ed859915c106464ee90fc7afbeef612b5db65f564d0cdfbacc197542ad2cff88cc970aa1600887b8c33e4d67cbc6ad83a70b5356a8e3e8ec377ed86f5c01541319cbc4fd8ae490fb3a7c4c32b7dfb55055d2b586febef23cd5ca379531f1aca65671e4892ebe3d881e134afd43642dbed9cca572d3653386c8ca45a49f2b2cc30b9eb052cf37a52d6335427667a0d3d8918c6bd7d98625a56057e193312bebb2f1e1f8f5aa2125450fff57c6be4642b5b1a1e8917e4a927c00a318287748c399631e085a3cc2e06cd2a497c960d1e00faa0ead20c1bb1bd27ac2cd4011952047812bc2ba711f04f8eaa232073f8297bf38d903e8c74bec1301ca424b9d2f986ed3323c745e39caf4a7d8e49b44ffcca3a8a06a1f67b02a9edfd62078836ecc8cb8380d6596b42b6fa8ec2cd5fa2671ea5a7d96513d67c41aa7890f10c0581fa7ec5ad7d4d995873f8f79c44b7cc03afc23491b9b6e8a5ff5d10761f9a3128289fe3f7d146856a7a228f3c47bd3431292f856569bf4b38ede8ae73925d7f3ac713e7ee44b8fbbc5fcdd1a079d3ee04a157024d2e4d76ce500f1627d6d1a478a30c6b3d83d2160311e0c3a99346a5d737689352e47bc8e129249c6b77d941275c54691ed9898d9fdb46a5c49b8aed00d2da49c04bf52d979d5506e478c4807e0fba836acb2c3e1b4447ce0a665845593d05d68df7ed12953a98d00c75681808d4ec4a721c27bd7c3f86d9066cb3536eeeb2ce31a9e1dd596a7b6ce65ac8fb33ed0221208f7c5257e608b6e56be78695b869b3f8e72154ea3a8e1c6ca7012393525797be54c91c427110350f863c632e49de3e9236a977084d3b430d2e31112a538b7c19668844241ca4b330f4b2a63ca864a3515da24585dca98350c91f9733ee1990065f68cde9d6287df6fc819fccfc4888437b39731acf09ed083204fe8ee62ac4d07def01e9d91b4c3fc268869b530285afbb5e30b52e8d22869960a47d9b85c3ee2f9b69bdbca0b0a14e04f89c12eb780ebe61d082353822edff5fd0d8e7bfdcb2e5fe07443d0bd7e923608ace5117ae33417dbd8fb6ef352b05f46fd8b1cd9ccc86ddb266fbeb14070f356c35e754b6b38d02ae2252803a9893abd3f57d114e17cc16a1ad54664b34df1d8073dbb8836176e6010a02d5bb5417d83e650fa15f02195c4ce5093788b3fefe66653160aec7107bc6563370941e1ccb88b41fa69e899ccbc60ba83bd91d5f2cc3e165a436fa70d35c105c70e28ba688ff2a47c81a8123393b2c4e21e9ef2df98e59bd4f88070b30f606f4bb89329162163f829185c428a344c79cb5fe9c709207506441b6d01181ea94e5b70cb553fd8c42e9ceb47ed294bd8eceade2b4de531f675b928efd6754e319e58678a4840fc08be115ffe397f90b57722afa789df00a0fc398a336f048215db096ffa46150e358e98ba82cb7cd8b4d19213069eba0b895d47adb9db3f41dc87a8bb987267c7d78d5d5965b9a72c8f76d26a12cc996e1973b992621db3ff4334e32022d1f911fea02cd81d0ebfc7af790f0912a32900ca26dccbd69d95010fd15b0ccba86870dfb202d11d4f3b2d767996aca171a9a856ef270657ecf104d979698a5ab335fa70c3e5b0684847166a4254a6fb15d867567e8914718d1d00c31f8b52f5a169656eeeb73f413eb1c07a506569df81203d9a3112b816c51ec0d6895b0997d7c4bc2fd3cf01604d7cb9672a80041a4808806b646e8d9337f7fab657fed895bbbb74847fcdf6615295dedfc579103bb01b38ac09ba4ca1adbd4accd080203b1d46d1afdf5b1134924bbfd15c7937f4cb2353b2ab62c65549de82b2eedb9fb31aca1d7ffaa678c73f23138df9673be78b2a557ae497fda01286fd51182c2c6e0492f8d4bd0fb089385c003b97d6eb980da5900ceaa1e044ab564e466af4e78569a9d2dd30c3441b273a3a43fe6c52d35a1913c5683cce18b3ea1dda3bbe22c36ffdaea956a12cb130144aed9f82aa684f2949c7fa563282dbbfc8c7b67f275e32cbd5ee6351359a4f33047333803204d65fe59dcb90d3bda19ccb1ca2f81d1b69124fe5be6920a066fef846e292d4168187f6a0cc9461d32298fd53a5d28be052d699bc05933aba0b42f6fa55b5a35d4a4153cda8dc58ebfff6985b327d0773b897086582af2e1522f0a517f6e6f32f475b4824ffd6e3141df976fd32ac7706f1615a7f76f0c60f43078aeacc00bb1e8fb86583e1c40957a5f27cb6a0e51efa9f2a3b5875c57aa626a1e4d17904fbc6e3859eb630fc2d7e9a95bf4e629538ca21720250b59aba307b71eb68effc37555a690bfa6b472b33e5ba24d08ae7ce8eff37cfb8303bcb34f54c161aaa9c65676a1ee2449651f8d9e425aedfa4e118dfa249ce933db005850a0a91d5d5087f0cf465c3dc6d3c5daddbe8fda79f3aa22135203447ced25dc3a319d6117aa6c65f7437b21ff8393d55ae96e15e7b3c30360c9afe03d126f8e9fb11661976d46c9e643e5fc6892f04c2e7f459870c43630e6f649cec32d5ee99a6e4949b69872008019d7f47ff49585b199a369226c4257f1a8bc76acf868d1277bfc6352c84ba91725786789cc47bd031499ea6a0ae45a192b970f91ba29ce402be8acd98560f2010ceedf4e65fc9c5bc43788aedc27e0597127911f5216c34dc7e7673dad082774bc0fe76221678b15b12a14d3c88d119a80ca00c40c3d57ac90188050a3e5f03259aa35d029f84bc6a1e1bcbc817ae6e94774ba25ba4c8b6c6af368637702ba109b3d5174924b559a4993671aeda583cc1e5ca224a123b3676173d7ad300f6f447249402e9d345afe4dac39f4add79fe3258640c0bd12e89f8821ef78e87a4300a128d742a2ce8c39747f28d83f49c291efa4bd123d3f621c9ff4c8a230db601df63c16910e9f8202d7788355fec921b2b1031252fbd5b13ad9cc3c855613ae8e0cb77629c0448fce0c5d31fcc301909f82fd0aa1c5bfa62af5c96395c6009a5edd797473750f7269082591ab78a842d592394688a721214a640c0cc61cc053322b64730fb40bfa4d1029e5b137582b24fbe910d74ee56b3fde2085562c5038a80aca8925f0d9a02f89ec9a8df6d5ce79905675c8131a36935045a505e764b77d0309765afed079d5399b4582dfddf2cf43552e7222702716e47cbca1b2bcaa4a2ff604cd9012f6b579551b0f52b23f46ac325b65643b04e6b3bc64a1037180dfa11c4353bb5bf4fcf0c6406eeb951c63cafbf83c9008df072bcf532e110792fc761cd8a97ab6f077788adb730be3a9f490a50efa2b47be2edf577720017cdc892dcdd03109d98f17836a3a541bce48a2e5a0d116d46e7217839ed58489394d17c1811b3d056a2cbc91b675768f5ca83ab81c2c623b50ce4bd91e1e2935cdb8617e03042c1ef161bf020f03437a34c140972d382086ed18ee88a9501a25504725111fb70a6271f0526a6b3f7774a874f508283be6a44f650d68ac57280bc0cc365c9c05a8809b0185b7827ceda8facd4f1ce23f326a9339ce656fa1d6885816445e3f37c48a19e47488290c767c241830b738b87f8332170eac38789c8f67194e26c63f01a10b7c26fa5fac14cf3c0cf2f34c84a580b8732bd868c188a35111348ec4e0b8db6e81cda2255196882400b3af7152baebc2b7cc9c3c9746d3526e4d2894f58998107243479c669812a026bd2d2df6f18cd82c1ba9d3feb2a3f0ac58ed12ee43c0f154e5126447a3bc78e7d7f4f3a554e15999c3e8e9a7f2808dc9fcc0a43dde76d601fb9d979d53c4b408a0e55e785f52acce63528fa2850308535f709b59674e7a78598281da0232241d6aa8886990eef6ee088e8ade607c71e0940fbc99d474fbeb21b1a1c3145bb9154738422fdc5247469d2d4f9b4196eb5aaf15ffd1c75564d63341c988e8cbee67422b5f17e0c65f71e49a08dc07307562fc3cfb3dffdf58dce2023cdf4ca4c1a0f053b5b425d12226aeb557c76d5a615c7dac19cab81c1d05ced91ee006cd20bfbd63c9cf485041118da0bcd7fe524cdfb835640a73d2c3066164f65c0a9de3fb534f835035d93b3d8b350d5922d346c98ce5f0ff7dcc8084b9e1da0c3ec3987266cfa9b88fa6a7873cdc4fef24b3838a91d0f939dd5a05092da3b616665a415e9828232473f1951f02285166c76f2e947a0525b083095890f4890a7a741931e3ecc6e9b51436877bfdc1179a72e1a0566b77711b357709bf8b42847871fb9af95a382a8827e511062da5fe0a5b12c8684ee55c2a7f999bcf7a381f01a82056985431cf3fde689a5460ba49b63506642900fba18e7193efd862b08c690974836a615547a951a4f09d6e9001cce0f89a07219367c384a487c90c267ce3b956b53cc75794f482a599f0a94be7484fefdab9d9a165a77463d89e8cf211a034a5cd047a989a047c6675e955333d89e54475e1ec6c371327e275d17e7cde3801912167ec65e927e6906b36bf758eda4d2467f2655a05ba93bbec7bc8edeb6da543446413c8777d0074b15688deadf8aad009cf78155b9dc1db1c2bf4253fd85513036fa8d99dc004b4ab515bb556c2a571937ef67f4a02b3afc27d95ef354d4afa57cf8313f9a53b74db18531a5ff1c5045883198a4d303b6a5807cc683968da64dae7c0e071ca7bc8c180b5cfe38ee5759c4d5f73dee1cbe0196633a88d8058bc2d2e52cdc638c447eb355255c4d5dd72ee27c07ff8543b28b965181041b1248ffcd6d47ee640224faff364a8c0510079d279213bb3e9cf69ddbdb2d1d3b21b3821a845bb0a05e61f94427fb334225b9e92c20019d75c284f4f8daef52c536e02280a63b9303b63162397ac876ffc8da71f2f183695c25de9192aabde76bd769105a42b320be6fe2091d76d489c4c8b0eadca4d49223c0fdfabc80f5819019365c05949290ae9d94a35209ea13f624aadc992e4b6447c7ae5b883be2d46e18cd82ae294b1708b96b2beebce3f324c75729f067b86392a9b73b608fef1dcf67474affaf2bb74530ced1348c46dba0702a8d301090cbaeeb78565e9469a40314eb30b079156504af75bc4a4eaf3c558a3639140f827d9b4c6c708f2fed7eea9c673cd90bec55f04efc9b8e53c904bc202e6c2641785853be5aecd53af9caffcbe00140f28f304ba56d64e1c362a7518d4ea0891be61b81b15ab1faa04c2f177d0c6125be10beddd91b8022ea6ab432d2bf624c62f8e644c2431b9d96195b166d7786947de20aa5da693f7a707bc19d36b07bcdd2be226dae783630796b5cada38fa87b0b47a6227428f85a095721a3b937ebb2bdb5c3c950213f30c7b6b59139573b63405009326bfb27e9f3848aa2a2793c8c36c8c9d19fcadaf534d11961c76f99985a4ab2d783549fb2a1b01ca04ba382dbad5b3634e8b3627a8c2916f9dc4583125aa10f8adb0d646e2ec523264763c72bc6d2bef20f9ace11ba723ca268b8391acbd291d638508415935c6cefcf453930dc9e3b4a7ebaa74313f4eb396c693c4d4583d15fd59788f3da08d6af13b811a32d904ec8874556662c158aadcf3b81f16f757ad841df2efceef041ec9821f32435fc8e410f52c1f6bbe58ecc6c297449c4099767bc37826d60f75fe1d4a26a28360448b41fc2a7b32d410fc0c51f54bc1fc6631e6077c86b72e1ba9028f6ad7b6640f11de5f12aa629e74647dd2e39195bc03971db38c047ae5b53b1a1b5890edb984c7d9754d5113e3ea265ec4cf8ec5a037ea05fe281eac313d48ac7b2c6073a1835b9c5bf14c9336b6c97b3633152f5523a928a8f65867cc5d40d74a965f954b3116c7adad55d29c9499bb8a3bc742fdde91574f2901bd02bf396a5ce70e2ff459d8411817455ed8f905b67ddb69797bcd65354561895234b41bfa580d6b745169cd266a95e60d96a86c203cd7d0c0ad79b4cbc498c7b39e8dfe202f01547448ab21f9a78514177790f8f46efed1c729a7779bff7ed0b2f054013dda03769637d80b56ab1558e09165d7906e43dcb9dd522a85f3561e312c1eb8921c262fdeb6b01ec2fdee8de8eaeeeeb3f7c6ac32264e6d49de0012ae505dc7e0cac6a3e2d339e9c072eadb7f29cd6ac51a00e2eb5b8623f98164aebfcb22748d070ac9aa7b91cd22d303e1ad0650bfa56c8a5d3e44f965ea539ed93016486e1235c51acbccfbe93be6d393c08cbe1d2fd5ad81e576936d5ba487e38059126df0c276aceae45c566c58e6297b79f7b7103615eaa13641fb907e810646fac33b5d8f4a0140d33eba0e127dedd04a31fd69366d3a67ce03f6f6d6df0646fcc81c56656710fddb3838d2fe16f04abebe9a9969a0afb96d71ef9bd64a8b90b0cac00cd93e7e17c7a6aee748c85b3de4b614469fb69ca07b3277d5ec0245b2e119abc316af5d0f22ed454869de0cd99da59066b46e21e6e47f8cbb13e3c8b14d29fec56a75ade774161ef4c0e96978e736366345e54a6ed86edc88e06a33ed21c3f5c9b5e1194f86b44f2390ec876f04352d56690d63f6b696e9a04b68299b7ac1b6ec2706027218545b2e4a617b5d089c06f5354c98bae0c57be4695abca1944e5f66ba2dcc66c6e2ffedb3adc10be9b3332cdb6e54cb3ad2416236abd746c4487c75a60b5c88c67948139c2eb84645b58b75c92cfc26a36a65c87a06774ad1ed5ac48ab61c0af8f1b3b56e678a322893bfad6d3173b2d046e5a2717b5f5528cbc5ebd3ddd424249021b8ce890f6c0606ad9567c045c10a74b26fce5a02cb752cf71fa99b52f4aa0f3a70efc6f7f15a617f07bca37c4b4a2d7aa51fe13858e0397ec48561f2677bc36d1d793bd868581d87de4ce2f8caa9626b0126c7d31c99d017f4d920a45875fb0dbc8ef3f0736ffbf2f7fd579ff0d7ead39e66c4be9ca45b42467626f388b1cdf4778ce1afd89a2876c684fd800ae0b2add8f9972d690ab71cc8b0616d89acddea9a0b704546045b32ac1a561faae2db708edcc7166258b5125b73c5be060797421a0231221d34f88bc39196b5af889a45e0e15360f25ef8e2b995a88d6dab74598980b2c523265acb70dac6f95698fea3f1bcdc281bf62f1a700a2ecf3c3c8b39331e18e66d7dcd65407cda053e5c119ed51537274074ae36eff938a088cce58cbf3c1b30717698e8edef555d2f47f1da25aa7d79fe94f42f4f687ab97c3dd4b2960a366a3154ff0a3267e365d191543d1e693ea78cd59b15746078bfce0992b132f7298fa407aa788bb764138d867a833d3355ea95c1aa40f5204b5f0c359e63c63b46996c4cbda1332a05e721e17ba3888b84804c9ca467645aa902bd04a7355a551f4dec73b3aa872956d3f13820701cf4290d437ae17a443c8f92993b6c9125687b38cf11c01220e3a71efebd98d7889b21d6598e869717b5e38d17b80fbf1f3aa51ac170a70752f86a397721e7c7a00adf7dce48ef30072fad589b3d41190c40dde96be668141a6ecfb878ec17b249a589347f47b478d955e1305bb149af7dc339a52a51ea4406ae4a71c5020a1337cc8bb77252ca8edaf220cd9bd8bf68f1cef2dee6e436ab5e50d6a8ad29d55e2027afec3ff14a8332765923de9fdf191141f21faf2afeb0e0ba9b7645807c2d82484a798d8b4da648dfb66d221a102460f94903d5f8165d3c2e983dbc7c5a08733993b0878ff75703022c7f92313f1614b28e079d9ce157b4c374da967857d7204a0ccab577ced984d2f6faceda34fb45be611d3f9639165caa9018dc19574378c07f089c88118a7e68f4465b76c4f35c3d28cb9cfbbc7cf4795f0630dcc0632be6fc1a9c3fb6e73bf671f9e447ad48bb03b74bf64863b6e";
        String encrypt = decrypt(sm2KeyModel, data);
        System.out.println("解密后数据：" + encrypt);
    }


    private static void testJava() {
        Sm2KeyModel sm2KeyModel = generator();
        System.out.println("publicKey:" + sm2KeyModel.getPublicKey());
        System.out.println("privateKey:" + sm2KeyModel.getPrivateKey());
        String srcData = "[{\"name\":\"System\",\"path\":\"/system\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"系统管理\",\"icon\":\"system\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"User\",\"path\":\"user\",\"hidden\":false,\"component\":\"system/user/index\",\"meta\":{\"title\":\"用户管理\",\"icon\":\"user\",\"noCache\":false,\"link\":null}},{\"name\":\"Role\",\"path\":\"role\",\"hidden\":false,\"component\":\"system/role/index\",\"meta\":{\"title\":\"角色管理\",\"icon\":\"peoples\",\"noCache\":false,\"link\":null}},{\"name\":\"Menu\",\"path\":\"menu\",\"hidden\":false,\"component\":\"system/menu/index\",\"meta\":{\"title\":\"菜单管理\",\"icon\":\"tree-table\",\"noCache\":false,\"link\":null}},{\"name\":\"Dept\",\"path\":\"dept\",\"hidden\":false,\"component\":\"system/dept/index\",\"meta\":{\"title\":\"部门管理\",\"icon\":\"tree\",\"noCache\":false,\"link\":null}},{\"name\":\"Post\",\"path\":\"post\",\"hidden\":false,\"component\":\"system/post/index\",\"meta\":{\"title\":\"岗位管理\",\"icon\":\"post\",\"noCache\":false,\"link\":null}},{\"name\":\"Dict\",\"path\":\"dict\",\"hidden\":false,\"component\":\"system/dict/index\",\"meta\":{\"title\":\"字典管理\",\"icon\":\"dict\",\"noCache\":false,\"link\":null}},{\"name\":\"Config\",\"path\":\"config\",\"hidden\":false,\"component\":\"system/config/index\",\"meta\":{\"title\":\"参数设置\",\"icon\":\"edit\",\"noCache\":false,\"link\":null}},{\"name\":\"Notice\",\"path\":\"notice\",\"hidden\":false,\"component\":\"system/notice/index\",\"meta\":{\"title\":\"通知公告\",\"icon\":\"message\",\"noCache\":false,\"link\":null}},{\"name\":\"Log\",\"path\":\"log\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"ParentView\",\"alwaysShow\":true,\"meta\":{\"title\":\"日志管理\",\"icon\":\"log\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"Operlog\",\"path\":\"operlog\",\"hidden\":false,\"component\":\"monitor/operlog/index\",\"meta\":{\"title\":\"操作日志\",\"icon\":\"form\",\"noCache\":false,\"link\":null}},{\"name\":\"Logininfor\",\"path\":\"logininfor\",\"hidden\":false,\"component\":\"monitor/logininfor/index\",\"meta\":{\"title\":\"登录日志\",\"icon\":\"logininfor\",\"noCache\":false,\"link\":null}}]}]},{\"name\":\"Monitor\",\"path\":\"/monitor\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"系统监控\",\"icon\":\"monitor\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"Online\",\"path\":\"online\",\"hidden\":false,\"component\":\"monitor/online/index\",\"meta\":{\"title\":\"在线用户\",\"icon\":\"online\",\"noCache\":false,\"link\":null}},{\"name\":\"Job\",\"path\":\"job\",\"hidden\":false,\"component\":\"monitor/job/index\",\"meta\":{\"title\":\"定时任务\",\"icon\":\"job\",\"noCache\":false,\"link\":null}},{\"name\":\"Druid\",\"path\":\"druid\",\"hidden\":false,\"component\":\"monitor/druid/index\",\"meta\":{\"title\":\"数据监控\",\"icon\":\"druid\",\"noCache\":false,\"link\":null}},{\"name\":\"Server\",\"path\":\"server\",\"hidden\":false,\"component\":\"monitor/server/index\",\"meta\":{\"title\":\"服务监控\",\"icon\":\"server\",\"noCache\":false,\"link\":null}},{\"name\":\"Cache\",\"path\":\"cache\",\"hidden\":false,\"component\":\"monitor/cache/index\",\"meta\":{\"title\":\"缓存监控\",\"icon\":\"redis\",\"noCache\":false,\"link\":null}},{\"name\":\"CacheList\",\"path\":\"cacheList\",\"hidden\":false,\"component\":\"monitor/cache/list\",\"meta\":{\"title\":\"缓存列表\",\"icon\":\"redis-list\",\"noCache\":false,\"link\":null}}]},{\"name\":\"Tool\",\"path\":\"/tool\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"系统工具\",\"icon\":\"tool\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"Build\",\"path\":\"build\",\"hidden\":false,\"component\":\"tool/build/index\",\"meta\":{\"title\":\"表单构建\",\"icon\":\"build\",\"noCache\":false,\"link\":null}},{\"name\":\"Gen\",\"path\":\"gen\",\"hidden\":false,\"component\":\"tool/gen/index\",\"meta\":{\"title\":\"代码生成\",\"icon\":\"code\",\"noCache\":false,\"link\":null}},{\"name\":\"Swagger\",\"path\":\"swagger\",\"hidden\":false,\"component\":\"tool/swagger/index\",\"meta\":{\"title\":\"系统接口\",\"icon\":\"swagger\",\"noCache\":false,\"link\":null}}]},{\"name\":\"ContractMange\",\"path\":\"/contractMange\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"合同履行管理\",\"icon\":\"zip\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"Contract\",\"path\":\"contract\",\"hidden\":false,\"component\":\"contract/index\",\"meta\":{\"title\":\"合同信息列表\",\"icon\":\"documentation\",\"noCache\":false,\"link\":null}},{\"name\":\"ContractBacklog\",\"path\":\"contractBacklog\",\"hidden\":false,\"component\":\"contract/contractUser/contractBacklog\",\"meta\":{\"title\":\"待办合同任务\",\"icon\":\"log\",\"noCache\":false,\"link\":null}},{\"name\":\"ContractFinishlog\",\"path\":\"contractFinishlog\",\"hidden\":false,\"component\":\"contract/contractUser/contractFinishlog\",\"meta\":{\"title\":\"已完成合同任务\",\"icon\":\"checkbox\",\"noCache\":false,\"link\":null}}]},{\"name\":\"Databank\",\"path\":\"/databank\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"资料库\",\"icon\":\"documentation\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"Catalogue\",\"path\":\"catalogue\",\"hidden\":false,\"component\":\"databank/catalogue/index\",\"meta\":{\"title\":\"资料库目录\",\"icon\":\"documentation\",\"noCache\":false,\"link\":null}},{\"name\":\"Data\",\"path\":\"data\",\"hidden\":false,\"component\":\"databank/data/index\",\"meta\":{\"title\":\"资料库数据\",\"icon\":\"documentation\",\"noCache\":false,\"link\":null}}]},{\"name\":\"Bpm\",\"path\":\"/bpm\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"工作流程\",\"icon\":\"server\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"Manager\",\"path\":\"manager\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"ParentView\",\"alwaysShow\":true,\"meta\":{\"title\":\"流程管理\",\"icon\":\"#\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"ActCloudForm\",\"path\":\"actCloudForm\",\"hidden\":false,\"component\":\"flowEngine/views/bpm/actCloudForm/index\",\"meta\":{\"title\":\"流程云表单\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"ProcPropCategory\",\"path\":\"procPropCategory\",\"hidden\":false,\"component\":\"flowEngine/views/bpm/processProperties/category\",\"meta\":{\"title\":\"配置类别\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"Model\",\"path\":\"model\",\"hidden\":false,\"component\":\"flowEngine/views/bpm/model/index\",\"meta\":{\"title\":\"流程模型\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"Process-properties\",\"path\":\"process-properties\",\"hidden\":false,\"component\":\"flowEngine/views/bpm/processProperties/index\",\"meta\":{\"title\":\"扩展属性定义\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"Task-admin\",\"path\":\"task-admin\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/work/admin\",\"meta\":{\"title\":\"管理员查询\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"Formconfig\",\"path\":\"formconfig\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/formConfig/index\",\"meta\":{\"title\":\"表单环节配置\",\"icon\":\"#\",\"noCache\":false,\"link\":null}}]},{\"name\":\"BpmTask\",\"path\":\"bpmTask\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"ParentView\",\"alwaysShow\":true,\"meta\":{\"title\":\"任务管理\",\"icon\":\"#\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"P-create\",\"path\":\"p-create\",\"hidden\":false,\"component\":\"flowEngine/views/bpm/processInstance/create\",\"meta\":{\"title\":\"新启流程\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"BpmnTaskOwn\",\"path\":\"bpmnTaskOwn\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/work/own\",\"meta\":{\"title\":\"我的流程\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"BpmTaskTodo\",\"path\":\"bpmTaskTodo\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/work/todo\",\"meta\":{\"title\":\"待办公文\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"MyUserGroup\",\"path\":\"myUserGroup\",\"hidden\":true,\"component\":\"flowEngine/views/bpm/myUserGroup/index\",\"meta\":{\"title\":\"我的分组\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"ActMyTaskDelegate\",\"path\":\"actMyTaskDelegate\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/work/myTaskDelegate\",\"meta\":{\"title\":\"我的委派\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"BpmnTaskDone\",\"path\":\"bpmnTaskDone\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/work/done\",\"meta\":{\"title\":\"已办公文\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"BpmnTaskFinished\",\"path\":\"bpmnTaskFinished\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/work/finished\",\"meta\":{\"title\":\"办结公文\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"BpmnTaskCopy\",\"path\":\"bpmnTaskCopy\",\"hidden\":false,\"component\":\"flowEngine/views/workflow/work/copy\",\"meta\":{\"title\":\"抄送公文\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"Comment\",\"path\":\"comment\",\"hidden\":false,\"component\":\"flowEngine/views/comment/index\",\"meta\":{\"title\":\"常用意见\",\"icon\":\"#\",\"noCache\":false,\"link\":null}}]}]},{\"name\":\"ExecuteMange\",\"path\":\"/executeMange\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"执行力\",\"icon\":\"monitor\",\"noCache\":false,\"link\":null},\"children\":[{\"name\":\"Task\",\"path\":\"task\",\"hidden\":false,\"component\":\"exec/task/myTask\",\"meta\":{\"title\":\"我的任务\",\"icon\":\"documentation\",\"noCache\":false,\"link\":null}},{\"name\":\"MyBacklog\",\"path\":\"myBacklog\",\"hidden\":false,\"component\":\"exec/taskUser/myBacklog\",\"meta\":{\"title\":\"我的待办任务\",\"icon\":\"documentation\",\"noCache\":false,\"link\":null}},{\"name\":\"MyFinished\",\"path\":\"myFinished\",\"hidden\":false,\"component\":\"exec/taskUser/myFinished\",\"meta\":{\"title\":\"我的已办任务\",\"icon\":\"documentation\",\"noCache\":false,\"link\":null}},{\"name\":\"TaskUserAttach\",\"path\":\"taskUserAttach\",\"hidden\":true,\"component\":\"exec/taskUserAttach/index\",\"meta\":{\"title\":\"用户任务办理附件\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"TaskAttach\",\"path\":\"taskAttach\",\"hidden\":true,\"component\":\"exec/taskAttach/index\",\"meta\":{\"title\":\"任务附件\",\"icon\":\"#\",\"noCache\":false,\"link\":null}},{\"name\":\"TaskUser\",\"path\":\"taskUser\",\"hidden\":true,\"component\":\"exec/taskUser/index\",\"meta\":{\"title\":\"任务办理用户\",\"icon\":\"#\",\"noCache\":false,\"link\":null}}]}]";
        System.out.println("加密前数据：" + srcData);
        String encrypt = encrypt(sm2KeyModel, srcData);
        System.out.println("加密后数据：" + encrypt);
        String decryptData = decrypt(sm2KeyModel, encrypt);
        System.out.println("解密后数据：" + decryptData);
    }
}
